package com.itheima.tlias84.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlias84.dto.ReportStudentDTO;
import com.itheima.tlias84.dto.SelectStudentDetailDTO;
import com.itheima.tlias84.dto.SelectViolationDTO;
import com.itheima.tlias84.po.InsertStudentParam;
import com.itheima.tlias84.dto.SelectStudentDTO;
import com.itheima.tlias84.entity.Student;
import com.itheima.tlias84.mapper.StudentMapper;
import com.itheima.tlias84.po.SelectStudentPageParam;
import com.itheima.tlias84.po.UpdateStudentParam;
import com.itheima.tlias84.po.UpdateVioStudentParam;
import com.itheima.tlias84.service.StudentService;
import com.itheima.tlias84.utils.ResultUtil;
import com.itheima.tlias84.vo.PageVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 学生服务层接口实现类
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    StudentMapper studentMapper;

    /**
     * 查询所有学生
     * @return List<Student>
     */
    @Override
    public List<Student> selectStudentAll() {
        return studentMapper.selectStudentAll();
    }

    /**
     * 分页查询学生信息
     * @param param 查询条件
     * @return 是否成功
     */
    @Override
    public ResultUtil selectStudentByPage(SelectStudentPageParam param) {
        //分页
        Page<Object> page = PageHelper.startPage(param.getPage(), param.getPageSize());
        List<SelectStudentDTO> dtos = studentMapper.selectStudentByPage(param);
        PageVO<SelectStudentDTO> pageVO = new PageVO<>();
        pageVO.setRows(dtos);
        pageVO.setTotal(page.getTotal());
        return ResultUtil.success(pageVO);
    }

    /**
     * 添加学生
     * @param student 学生信息
     * @return 是否成功
     */
    @Override
    public int insertStudent(InsertStudentParam student) {
        //设置时间
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        int inserted = studentMapper.insertStudent(student);
        return inserted;
    }

    /**
     * 根据id查询学生信息
     * @param id id
     * @return 学生信息
     */
    @Override
    public SelectStudentDetailDTO selectStudentById(Integer id) {
        SelectStudentDetailDTO dto = studentMapper.selectStudentById(id);
//        System.out.println("回显学生id"+dto.getId());
        return dto;
    }

    /**
     * 修改学生信息
     * @param student 学生信息
     * @return 修改结果
     */
    @Override
    public int updateStudent(UpdateStudentParam student) {
        student.setUpdateTime(LocalDateTime.now());
        return studentMapper.updateStudent(student);
    }

    /**
     * 添加违纪信息
     * @return 结果
     */
    @Override
    public int updateStudentViolation(UpdateVioStudentParam param) {
        SelectViolationDTO student = studentMapper.selectStudentViolationById(param.getId());
        param.setViolationCount(student.getViolationCount()+1);
        param.setViolationScore(param.getViolationScore()+student.getViolationScore());
        param.setUpdateTime(LocalDateTime.now());
        return studentMapper.updateStudentViolation(param);
    }

    @Override
    public int deleteStudentByIds(List<Integer> ids) {
        return studentMapper.deleteStudentByIds(ids);
    }

    /**
     * 统计学生学位
     * @return 学位-人数
     */
    @Override
    public List<ReportStudentDTO> countStudentDegree() {
        return studentMapper.countStudentDegree();
    }

    /**
     * 统计班级人数
     * @return 班级-人数
     */
    @Override
    public ResultUtil countStudentClazz() {
        //TODO 新建一个DTO来接收链表查到的数据
        List<ReportStudentDTO> dtos = studentMapper.countStudentClazz();
        //拿出-班级名称
        List<String> clazzList = dtos.stream().map(ReportStudentDTO::getName).toList();
        //拿出-人数
        List<Integer> dataList = dtos.stream().map(ReportStudentDTO::getValue).toList();
        //转成json结构
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("clazzList",clazzList);
        jsonObject.put("dataList",dataList);
        return ResultUtil.success(jsonObject);
    }
}
